'use strict';

let DaoBase = require('./DaoBase');
let db = require('./models');
let D = require('./function');
let F = require('../../function');

var util = require("util");

function WikiDao(data) {
  data = data || {}
  data.model = data.model || db.wiki;
  DaoBase.call(this, data);
}

util.inherits(WikiDao, DaoBase);

/*
 *@todo 查询
 *@param  {Object}  query  查询的内容
 *@param  {Object}  opt    其他参数
 */

WikiDao.prototype.getByQuery = function*(query, page, pageSize, attributes) {
  page = page || 1;
  pageSize = pageSize || 50;
  let offset = (page - 1) * pageSize;

  let opt = {where: query, limit: pageSize, offset: offset};
  if (!!attributes) opt.attributes = attributes;

  let ret = yield  this.model.findAll(opt);
  if (!ret) return F.returnData(1, '查询失败');
  return F.returnData(0, '查询成功.', ret)
};

/*
 @todo 查询一条 并保存
 @query 查询条件
 @opt 参数
 */
WikiDao.prototype.findAndSave = function* (doc, opt) {
  let ret = this.getOneByQuery({orderid: doc.orderid});
  if (ret.error == 1) return F.returnData(1, ret.msg, ret.data);
  //let result = yield this.save(doc);
  return F.returnData(0, result.msg, result.data);
};

module.exports = WikiDao;
